//
//  ReadWorksViewController.m
//  Lesson_Read
//
//  Created by 李洪鹏 on 15-4-29.
//  Copyright (c) 2015年 ___蓝鸥科技公司___. All rights reserved.
//

#import "ReadWorksViewController.h"
#import "ReadWorks.h"

@interface ReadWorksViewController () <UIWebViewDelegate>

@property (nonatomic, retain)UIWebView *webView;

#pragma mark - 声明私有属性
@property (nonatomic, retain)NSMutableArray *allDataArray;
@property (nonatomic, retain)NSMutableData *allData;
@property (nonatomic, retain)NSString *htmlString;


- (void)loadData;


@end

@implementation ReadWorksViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //加载数据
    [self loadData];
    
    self.webView = [[[UIWebView alloc] initWithFrame:self.view.bounds] autorelease];
    
     [self.view addSubview:_webView];
    
    
    

}


- (void)loadData
{
    self.allDataArray = [NSMutableArray array];
    
    //获取网络数据
    NSString *str = [NSString stringWithFormat:@"http://api2.pianke.me/article/info"];
    NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
   
    NSString *paramDataString = [NSString stringWithFormat:@"contentid=%@",_contentId];
    NSLog(@"ceshi%@",_contentId);
    NSData *paramData = [paramDataString dataUsingEncoding:NSUTF8StringEncoding];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:paramData];
    
    [NSURLConnection connectionWithRequest:request delegate:self];
    
    
}



#pragma mark -NSURLConnectionDataDelegate
#pragma mark 开始接受请求结果
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    self.allData = [NSMutableData data];
}

#pragma mark 接受数据
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    
    [_allData appendData:data];
}

#pragma mark 数据接受完毕
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    self.allDataArray = [NSMutableArray array];
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:_allData options:NSJSONReadingAllowFragments error:nil];
    ReadWorks *read = [ReadWorks new];
    [read setValuesForKeysWithDictionary:dict[@"data"]];
     _htmlString = read.html;
    [read release];
    
                     
    self.webView.delegate = self;
    
    [_webView loadHTMLString:_htmlString baseURL:nil];

    
    
}


- (void)webViewDidFinishLoad:(UIWebView *)webView
{


    [_webView stringByEvaluatingJavaScriptFromString:
     @"var script = document.createElement('script');"
     "script.type = 'text/javascript';"
     "script.text = \"function ResizeImages() { "
     "var myimg,oldwidth;"
     "var maxwidth = 320;" //缩放系数
     "for(i=0;i < document.images.length;i++){"
     "myimg = document.images[i];"
     "if(myimg.width > maxwidth){"
     "oldwidth = myimg.width;"
     "myimg.width = maxwidth;"
     "myimg.height = myimg.height * (maxwidth/oldwidth) + 90;"
     "}"
     "}"
     "}\";"
     "document.getElementsByTagName('head')[0].appendChild(script);"];
    
    [_webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
    
    
}






#pragma mark 发生错误,执行此方法
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"%@", error);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(void)dealloc
{
    [_contentId release];
    [super dealloc];
    
}



@end
